﻿using IceDog.UpdateDB.Model;
using IceDog.UpdateDB.Model.Data;
using IceDog.UpdateDB.Model.Enums;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace IceDog.UpdateDB.IRepository
{
    /// <summary>
    /// 基础仓储定义
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    public partial interface IRepository<TModel> where TModel : BaseModel, new()
    {
        /// <summary>
        /// 运维数据库上下文
        /// </summary>
        EFContext Context { get; }

        /// <summary>
        /// 添加模型
        /// </summary>
        /// <param name="model">模型</param>
        /// <returns>添加的模型</returns>
        TModel Add(TModel model);


        /// <summary>
        /// 添加模型
        /// </summary>
        /// <param name="models">模型列表</param>
        /// <returns>添加的模型</returns>
        List<TModel> Add(List<TModel> models);


        /// <summary>
        /// 更新模型，根据模型modelId值更新
        /// </summary>
        /// <param name="model">模型</param>
        /// <returns>更新的模型</returns>
        TModel Update(TModel model);

        /// <summary>
        /// 更新模型，根据模型modelId值更新
        /// </summary>
        /// <param name="models">模型列表</param>
        /// <returns>更新的模型列表</returns>
        List<TModel> Update(List<TModel> models);

        /// <summary>
        /// 删除模型，根据模型modelId值删除
        /// </summary>
        /// <param name="modelId">模型Id</param>
        /// <returns>删除的模型数量</returns>
        int Delete(int modelId);
        
        /// <summary>
        /// 删除模型，根据模型modelGuid值删除
        /// </summary>
        /// <param name="modelGuid">模型Guid</param>
        /// <returns>删除的模型数量</returns>
        int Delete(string modelGuid);
        
        /// <summary>
        /// 批量删除模型，根据模型modelId值删除
        /// </summary>
        /// <param name="modelIds">模型id列表</param>
        /// <returns>删除的模型数量></returns>
        int Delete(List<int> modelIds);

        /// <summary>
        /// 批量删除模型，根据模型modelId值删除
        /// </summary>
        /// <param name="modelIds">模型guid列表</param>
        /// <returns>删除的模型数量></returns>
        int Delete(List<string> modelGuids);

        /// <summary>
        /// 根据ModelId进行查找模型
        /// </summary>
        /// <param name="modelId">模型id</param>
        /// <returns>找到的模型，找不到为null</returns>
        TModel SelectOne(int modelId);

        /// <summary>
        /// 根据modelGuid进行查找模型
        /// </summary>
        /// <param name="modelGuid">模型guid</param>
        /// <returns>找到的模型，找不到为null</returns>
        TModel SelectOne(string modelGuid);

        /// <summary>
        /// 根据表达式进行查找出第一个模型
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>找到的模型列表</returns>
        TModel SelectOne(Expression<Func<TModel, bool>> expression);

        /// <summary>
        /// 根据表达式进行查找模型
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <returns>找到的模型列表</returns>
        List<TModel> Select(Expression<Func<TModel,bool>> expression);

        /// <summary>
        /// 根据条件进行查找模型
        /// </summary>
        /// <typeparam name="TSortField">表达式给定的排序字段类型</typeparam>
        /// <param name="predicate">条件断言</param>
        /// <param name="sortFieldSelector">排序字段选择器</param>
        /// <param name="sortOrder">排序方式</param>
        /// <param name="pageNumber">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>找到的模型列表和总数</returns>
        (List<TModel> results, long totalCount) Select<TSortField>(Expression<Func<TModel, bool>> predicate, Expression<Func<TModel, TSortField>> sortFieldSelector, SortOrder sortOrder, int pageNumber, int pageSize);

        /// <summary>
        /// 返回所有模型
        /// </summary>
        /// <returns></returns>
        List<TModel> SelectAll();

        /// <summary>
        /// 根据modelId判断模型是否存在
        /// </summary>
        /// <param name="modelId">模型id</param>
        /// <returns></returns>
        bool Exists(int modelId);

        /// <summary>
        /// 根据modelGuid判断模型是否存在
        /// </summary>
        /// <param name="modelGuid">模型guid</param>
        /// <returns></returns>
        bool Exists(string modelGuid);

        /// <summary>
        /// 根据modelGuid判断模型是否存在
        /// </summary>
        /// <param name="predicate">条件断言</param>
        /// <returns></returns>
        bool Exists(Expression<Func<TModel, bool>> predicate);
    }
}
